package org.bddall.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static java.lang.Thread.sleep;

public class LoginPage {
    private WebDriver driver;

    public LoginPage(WebDriver driver){
        this.driver = driver;
    }
    public void setUserName(String userName){
        WebElement e = driver.findElement(By.name("username"));
        e.sendKeys(userName);

    }

    public void setPassword(String password){
        WebElement e = driver.findElement(By.name("password"));
        e.sendKeys(password);
    }

    public void login(){
        WebElement e = driver.findElement(By.xpath("//*[@id=\"app\"]/div/form/div[3]/div/button"));
      WebElement ew =  new WebDriverWait(driver, Duration.ofSeconds(10))
                .until(ExpectedConditions.elementToBeClickable(e));
       ew.click();
    }

    public boolean isLoninSuccessful(){
        try {
            sleep(5000);
            WebElement ew =  new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div[5]/div/div[1]/button"))));
            ew.click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

       return driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/ul/div[2]/span/span/span[1]/a")).isDisplayed();
    }

}
